package cn.xingzhen.jfield.ui;

import cn.xingzhen.jfield.exception.MessageException;
import cn.xingzhen.jfield.helper.DialogHelper;
import cn.xingzhen.jfield.helper.ValidUtil;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author wenqiang
 * @date 2023/5/6
 * @comment ..
 */
public class RunAction implements ActionListener {
    private MainFrame frame;
    private DataField dataField;

    public RunAction(MainFrame frame) {
        this.frame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent evnent) {
        frame.showTextArea.setText("");
        try {
            this.createSource();
            ValidUtil.notEmpty(frame.dbHost.getText(), "Host不能为空");
            ValidUtil.notEmpty(frame.dbPort.getText(), "Port不能为空");
            ValidUtil.notEmpty(frame.dbUser.getText(), "用户名不能为空");
            ValidUtil.notEmpty(frame.dbPassword.getText(), "端口不能为空");
            ValidUtil.notEmpty(frame.dbName.getText(), "数据库实例名不能为空");
            ValidUtil.notEmpty(frame.tableName.getText(), "「表」不能为空");

            StringBuilder stringBuilder = null;
            if (frame.javaField.isSelected()) {
                stringBuilder = dataField.javaField();
            }

            if (frame.mbInsert.isSelected()) {
                stringBuilder = dataField.mbInsert();
            }

            if (frame.mbUpdate.isSelected()) {
                stringBuilder = dataField.mbUpdate();
            }

            if (frame.mbSet.isSelected()) {
                stringBuilder = dataField.mbUpdateSet();
            }

            if (frame.sqlResult.isSelected()) {
                stringBuilder = dataField.sqlSelectField();
            }

            if (frame.sqlInsert.isSelected()) {
                stringBuilder = dataField.sqlInsert();
            }

            if (frame.sqlDelete.isSelected()) {
                stringBuilder = dataField.sqlDelete();
            }

            if (frame.sqlUpdate.isSelected()) {
                stringBuilder = dataField.sqlUpdate();
            }

            if (frame.sqlSelect.isSelected()) {
                stringBuilder = dataField.sqlSelect();
            }

            if (stringBuilder == null || stringBuilder.toString().isEmpty()) {
                frame.showTextArea.setText("没有内容");
            } else {
                frame.showTextArea.setText(stringBuilder.toString());
            }
        } catch (MessageException e) {
            DialogHelper.warning(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            DialogHelper.error(e);
        }
    }

    private void createSource() {
        if (frame.dbType.getSelectedItem().equals("SQLServer")) {
            this.dataField = new MSSQLDataField(frame);
        }
        if (frame.dbType.getSelectedItem().equals("Oracle")) {
            this.dataField = new OracleDataField(frame);
        }
        if (frame.dbType.getSelectedItem().equals("MySQL")) {
            this.dataField = new MySQLDataField(frame);
        }
    }
}
